home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
fish
/
001-100
/
001-025
/
004
/
bison
/
calc.y
< prev
next >
Wrap
Text File
|
1995-03-17
|
1KB
|
113 lines
%{
#include <ctype.h>
int regs[26];
int base;
int val;
int printflag;
%}
%start stmt
%token DIGIT LETTER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%%
stmt : expr
{ val = $1; printflag = 1; }
| LETTER '=' expr
{ regs[$1] = $3; }
;
expr : '(' expr ')'
{ $$ = $2; }
| expr '+' expr
{ if ($1 == 69)
{
$$ = 2;
printf ("$1 became %d\n", $1);
$1 = 69;
}
$$ = $1 + $3; }
| expr '-' expr
{ $$ = $1 - $3; }
| expr '*' expr
{ $$ = $1 * $3; }
| expr '/' expr
{ $$ = $1 / $3; }
| expr '%' expr
{ $$ = $1 % $3; }
| expr '|' expr
{ $$ = $1 | $3; }
| expr '&' expr
{ $$ = $1 & $3; }
| '-' expr %prec UMINUS
{ $$ = - $2; }
| LETTER
{ $$ = regs[$1]; }
| number
/* | '?'
{ yydebug = !yydebug; }
*/ ;
number : DIGIT
{ $$ = $1; base = ($1 == 0) ? 8 : 10; }
| number DIGIT
{ $$ = base * $1 + $2; }
;
%%
static int eol;
int
yylex()
{
int c;
while ( (c=getchar()) == ' ') {}
if (c == '\n')
{ eol = 1;
return 0; }
if (islower(c))
{
yylval = c - 'a';
return (LETTER);
}
if (isdigit(c))
{
yylval = c - '0';
return (DIGIT);
}
return (c);
}
yyerror(s)
char *s;
{
printf("%s\n", s);
}
main()
{
for (;;)
{
eol = 0;
printflag = 0;
if (yyparse()) printflag = 0;
if (printflag) printf("%d\n", val);
while (!eol) yylex();
}
}